This section presents some useful Python libraries:
Note: a library is a collection of packages, but I have a habit of not differentiating between the two.
import matplotlib.pyplot as plt
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()
# changing the explode parameters
explode = (0, 0, 0.3, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()
# function to modify explode parameters
def explode_pie(a, b, c, d):
explode = (a, b, c, d) # only "explode" the 2nd slice (i.e. 'Hogs')
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()
# explode pie!
explode_pie(0,0,0.25,0)
More examples of matplotlib vizzes at matplotlib.org.
NumPy is library containing array and matrix data structures as well as methods to operate on these structures.
One thing to note is that NumPy is really fast because of how it uses pre-compiled C code in the background and how it vectorizes the data it stores.
Basics of Pandas:
import pandas as pd
# create python dictionary containing data
data = {
'apples': [3, 2, 0, 1],
'oranges': [0, 3, 7, 2]
}
# pass data to pandas data frame
purchases = pd.DataFrame(data)
purchases
# name rows of data frame
purchases = pd.DataFrame(data, index=['June', 'Robert', 'Lily', 'David'])
purchases
# locate June's order
purchases.loc['June']
SciKit Learn is one of the primary machine learning packages. Another is TensorFlow.
An article I found by a credit risk modeler pointed to some of the pros and cons of Python:
The author gave examples of the cons based on their experience:
Moral of the story: there are tradeoffs to using Python, and we may prefer to continue using more familiar tools where Python is not required.
SciPy expands on the linear algebra capabilities of NumPy. If a matrix-related feature is not available in NumPy, or if the available NumPy feature is slow, SciPy might provide a solution.
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(y=[2, 1, 4, 3]))
fig.add_trace(go.Bar(y=[1, 4, 3, 2]))
fig.update_layout(title = 'Hello Figure')
fig.show()
import plotly.express as px
df = px.data.gapminder().query("year==2007")
fig = px.scatter_geo(df, locations="iso_alpha", color="continent",
hover_name="country", size="pop",
projection="natural earth")
fig.show()
Source: https://github.com/xhlulu/dash_pivottable/blob/master/images/pivottable-demo.gif